//CourseMapper.java
package org.example.demo3.mapper;

import org.apache.ibatis.annotations.*;
import org.example.demo3.model.Course;
import org.example.demo3.model.Teacher;

import java.util.List;

@Mapper
public interface CourseMapper {
    //查询课程列表
    @Select("select * from course")
    List<Course> selectAll();

    //删除课程
    @Delete("delete from course where id=#{id}")
    int deleteById(int id);

    //添加课程
    @Insert("insert into course(name,teacher_id) values(#{name},#{teacher_id})")
    int insert(Course course);

    //查询对应id的课程
    @Select("select * from course where id=#{id}")
    Course selectById(int id);

    //更新课程
    @Update("update course set name=#{name},teacher_id=#{teacher_id} where id=#{id}")
    int update(Course course);

    //分页查询
    @Select("select * from course limit #{formIndex},#{pageSize}")
    @Results({
            @Result(column = "id", property = "id", id = true),
            @Result(column = "teacher_id",property = "teacher",javaType = Teacher.class,
                    one = @One(select = "org.example.demo3.mapper.TeacherMapper.selectById")),
            @Result(column = "id",property = "scores",javaType = List.class,
                    many = @Many(select = "org.example.demo3.mapper.ScoreMapper.selectByCourseId"))
    })
    List<Course> selectByPage(int formIndex, int pageSize);

}
